import SysAppEdit from '@/views/modules/system/core/SysAppEdit';
import SysAppManager from "@/views/modules/system/core/SysAppManager";
import { Util,Dialog } from "@lowcode/jpaas-common-lib";
import SysAppExport from "@/views/modules/system/core/SysAppExport";
import SysAppApi from "@/api/system/core/sysApp";
import SysAppCopy from "@/views/modules/system/core/SysAppCopy";
import FormResourceShareApi from "@/api/form/core/formResourceShare";
import AppImport from "@/views/modules/system/core/AppImport";
import ApplicationShareDetail from "@/views/application/ApplicationShareDetail";
import AppDownloadDialog from "@/views/application/AppDownloadDialog.vue";
import AppDevOpsManage from "@/views/application/AppDevOpsManage";
export default {
    methods:{
        edit(moveApp){//编辑
            var conf = {
                curVm: this,
                widthHeight: ['980px', '600px'],
                //可以调整zIndex
                zIndex: 200,
                component: SysAppEdit,
                title: this.$ti18('rxList.editBtn', '编辑')+"-" + this.comment,
                //传入到弹出组件的数据
                data: {
                    pkId: moveApp.pkId,

                },
                shade: true
            };

            this.handleAppEdit(conf);
        },

        handleAppEdit(conf){
            let self_=this;
            Util.open(conf,function (action,data){
                if(action=="ok"){
                    self_.loadData();
                }
            })
        },

        develop(record){ //应用开发

            let routeData = this.$router.resolve({
                path:'/appDesign/appHome',
                query:{
                    appId:record.pkId,
                    appName:this.$i18(record.clientName,'应用名称'),
                    appIcon:record.icon
                }
            });
            window.open(routeData.href, '_blank')
        },
        //获取应用的首个菜单
        getFirstMenu(menu){
            if(!menu || menu.children.length==0){
                return menu;
            }
            if( menu.children[0].children &&  menu.children[0].children.length>0){
                return this.getFirstMenu( menu.children[0]);
            }else {
                return menu.children[0];
            }
        },

        authorize(app) {  //应用授权管理
            Util.open(
            {
                    component: SysAppManager,
                    curVm: this,
                    widthHeight: ['1000px', '650px'],
                    title: '['+this.$i18(app.clientName,'应用名称')+']'+this.i18n('title.authorization','应用授权'),
                    data: {
                        curApp:app,
                        callback: function (message) {
                            this.$message.info(message);
                        }
                    }
                },function (action) {});
        },
        doExport(moveApp){     //导出按钮事件方法
            let appName=this.$i18( moveApp.clientName);
            let conf = {
                component: SysAppExport,
                widthHeight: ['480px', '300px'],
                title:this.i18n('title.derived','导出应用:') +appName,
                curVm:this,
                maxmin:false,
                zIndex:9969,
                data: {
                    appId: moveApp.appId,
                    appName:appName
                }
            };
            //弹出对话框
            Util.open(conf, (action,data) =>{
                if(action!='ok') return;
                //对话框确认后
                SysAppApi.doExport(data);
                this.$message.info(this.i18n('tips.downloading','正在下载中，稍候请前往浏览器的下载目录下查看！'));
            });
        },
        // 删除
        delById(id) {
            let self_ = this;
            this.$confirm({
                title: self_.i18n('tips.deleteInfo','你确定要删除当前应用数据吗?'),
                content: self_.i18n('tips.propose','建议先备份数据；删除之后，相关的数据将无法恢复！'),
                okText: self_.$ti18('list.deletion','确认删除','SysApp'),
                cancelText: self_.$ti18('rxForm.cancelBtn','取消'),
                okType: 'danger',
                onOk() {
                    SysAppApi.del({id:id}).then(res => {
                        if (res.success && res.code == 200) {
                            //当前的分类下，如果只有一个应用；删除后要切换页签
                            for (let item of self_.appList){
                                if(item.apps.length > 0){
                                    let _index = item.apps.findIndex(app=>app.appId === id);
                                    if(_index === 0){
                                        self_.tabActiveKey = 'all'
                                    }
                                }
                            }
                            self_.loadData();
                        }
                    })
                },
            });
        },
        //复制按钮事件方法
        copy(moveApp){
            let appName=this.$i18( moveApp.clientName);
            let conf = {
                component: SysAppCopy,
                widthHeight: ['900px', '600px'],
                title:this.i18n('title.copyed','复制应用:') +appName,
                curVm:this,
                maxmin:false,
                zIndex:9969,
                data: {
                    record: moveApp
                }
            };
            let self=this;
            //弹出对话框
            Util.open(conf,function (action,data) {
                if(action!='ok') return;
                //对话框确认后
                SysAppApi.copy(data).then(res=>{
                    if(res.success) {
                        self.$message.info(self.i18n('tips.copyLoading', '正在复制中，稍候请刷新浏览器查看！'));
                    }
                });
            });
        },
        /**
         * 复制登录地址.
         * @param app
         */
        copyUrl(app){
            let url = location.protocol +"//" + location.host + "/main";
            if(!url.endWith("/")){
                url+="/";
            }
            url+=app.clientCode +"/login";
            Util.writeToClipboard(url);
            this.$message.success(this.i18n('button.copyLoginInfo', "复制登录URL成功!") );
        },
        shareDetail(app){
            var self=this;
            FormResourceShareApi.getAllShareMyApps(app.appId).then(res => {
                if(res.success) {
                    let shareApps = res.data.apps || [];
                    if(shareApps.length>0) {
                        let shareTypes = res.data.shareTypes || {};
                        var config = {
                            curVm: self,
                            component: ApplicationShareDetail,
                            data: {
                                shareApps: shareApps,
                                shareTypes: shareTypes,
                                selfApp: app
                            },
                            title: self.i18n('title.shareDetail', '查看共享给')
                                + self.$i18(app.clientName, '应用名称')
                                + self.i18n(
                                    'title.is', '的'),
                            widthHeight: ['1000px', '720px'],
                        };
                        Util.open(config);
                        return;
                    }
                }
                self.$message.info(self.i18n('tips.shareEmpty', '没有共享数据！'));
            })
            //})
        },
        //分享按钮事件方法
        share(curRow) {
            FormResourceShareApi.getAllShareApps(curRow.appId).then(res => {
                let initData = res.data;
                if (!initData) {
                    initData = [];
                }
                Dialog.openSystemAppDialog({
                    curVm: this, widthHeight: ['1150px', '735px'],
                    data: {initSelected: initData, ownerApp: curRow, isDevelopApp: true}
                }, function (self, data) {
                    //保存共享信息
                    FormResourceShareApi.saveAllShareApps(data).then(res => {
                        if (res.success && res.code == 200) {}
                    });
                })
            });
        },
        add() {
            var conf = {
                curVm: this,
                widthHeight: ['980px', '600px'],
                //可以调整zIndex
                zIndex: 200,
                component: SysAppEdit,
                title: this.$ti18('rxList.newBtn','添加')+"-" + this.comment,
                shade: true
            };
            this.handleAppEdit(conf);
        },
        //导入按钮事件方法
        doImport(){
            let treeId=this.tabActiveKey;
            if(treeId == 'all'){
                this.$message.warning(this.i18n('tips.switch','请切换一个具体的应用分类！当前为“全部”应用'));
                return ;
            }
            let self=this;
            Util.open(
                {
                    component: AppImport,
                    curVm: this,
                    widthHeight: ['1100px', '620px'],
                    title: self.i18n('title.import','应用导入'),
                    data: {
                        treeId:treeId,
                        callback: function (flag, message) {
                            if (flag) {
                                self.$message.success(message);
                                self.loadData();
                            } else {
                                self.$message.error(message);
                            }

                        }
                    }
                },function (action) {
                });
        },
        downloadFront(){//前端代码下载
            SysAppApi.downloadFront();
        },
        downloadBackend(){//后端代码下载
            SysAppApi.downloadBackend({});
        },
        /**
         * 下载应用
         * @param app
         */
        downloadApp(app) {
            let self = this;
            Util.open({
                component: AppDownloadDialog,
                curVm: this,
                widthHeight: ['1100px', '620px'],
                title: self.i18n('form.downloadApp', '应用下载')+"-"+self.$i18(app.clientName,'应用名称'),
                data: {
                    app: app,
                }
            }, function (action) {
                if(action!="ok") {
                    return;
                }
                self.$message.info(self.i18n('tips.downloading', '正在下载中，稍候请前往浏览器的下载目录下查看！'));
            });
        },
        /**
         * devOpsManage
         * @param app
         */
        devOpsManage(app){
            let self = this;
            Util.open({
                component: AppDevOpsManage,
                curVm: this,
                widthHeight: ['1100px', '620px'],
                title: self.$i18(app.clientName,'应用名称')+"-DevOps管理",
                data: {
                    app: app,
                }
            }, function (action) {});
        }
    }
}
